#include "RegularOuter.h"
#include "ILevel.h"
#include "Record.h"
#include "Common.h"

namespace WLog
{
	RegularOuter::RegularOuter(ostream &out)
		: m_out(out)
	{
		
	}
	
	RegularOuter::~RegularOuter()
	{
		
	}

	bool RegularOuter::outRecord(Record *pRecord, ILevel *pLevel)
	{
		if (!pLevel->callBack_BeforeOut(pRecord, this))
		{
			return false;
		}

		if (pLevel->bOutFileName())
		{
			out(pRecord->m_strFileName);
		}
		if (pLevel->bOutFuncName())
		{
			out(" : ");
			out(pRecord->m_strFuncName);
		}
		if (pLevel->bOutLineNo())
		{
			out("(Line:" + l2str(pRecord->m_nLine) + ") ");			
		}
		if (pLevel->bOutTime())
		{
			out(pRecord->m_strTime);
			outendl();
		}
		
		if (pLevel->bOutName())
		{
			out(pLevel->getName());
			out(" \t| ");
		}

		out(pRecord->m_strMsg);
		
		if (pLevel->bNewLine())
		{
			outendl();
		}
		
		return pLevel->callBack_AfterOut(pRecord, this);
	}

	bool RegularOuter::out(string strOut)
	{
		if (m_out)
		{
			m_out << strOut;
            m_out.flush();
			return true;
		}
		
		return false;
	}

	bool RegularOuter::outendl()
	{
		if (m_out)
		{
			m_out << std::endl;

			return true;
		}
		return false;
	}
}
